//
//  TSDismissTransition.m
//  自定义转场动画
//
//  Created by 唐铁山 on 2018/9/13.
//  Copyright © 2018年 唐铁山. All rights reserved.
//

#import "TSDismissTransition.h"

@implementation TSDismissTransition
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.8f;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    
    UIView *containerView = [transitionContext containerView];
    
    UIViewController *fromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    [containerView addSubview:toVc.view];
    [containerView addSubview:fromVc.view];
    
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGRect finalRect = CGRectOffset(fromVc.view.frame, 0, screenBounds.size.height);
    
    toVc.view.transform = CGAffineTransformMakeScale(0.9f, 0.9f);
    
    [UIView animateWithDuration:0.3f animations:^{
        fromVc.view.frame = finalRect;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5f animations:^{
            toVc.view.transform = CGAffineTransformMakeScale(1.0, 1.0f);
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    }];
    
}
@end
